package org.example.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.example.base.BaseService;
import org.example.dao.UserMapper;
import org.example.exceptions.ParamsException;
import org.example.model.UserBo;
import org.example.service.UserService;
import org.example.utils.AssertUtil;
import org.example.utils.Md5Util;
import org.example.vo.User;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * @ClassName UserServiceImpl
 * @Author lidahu
 * @Description //TODO
 * @Date 2023/2/5 23:15
 **/
@Service
public class UserServiceImpl extends BaseService<User, Integer> implements UserService {

    @Resource
    UserMapper userMapper;

    @Override
    public UserBo selectUser(User user) {
        AssertUtil.isTrue(ObjectUtils.isEmpty(user), "参数为空");

        String userName = user.getUserName();
        AssertUtil.isTrue(StringUtils.isBlank(userName), "用户名不能为空");

        String userPwd = user.getUserPwd();
        AssertUtil.isTrue(StringUtils.isBlank(userPwd), "密码不能为空");

        User nameUser = userMapper.selectUser(userName);
        AssertUtil.isTrue(ObjectUtils.isEmpty(nameUser), "用户名错误");

        String userPwd1 = nameUser.getUserPwd();
        String encode = Md5Util.encode(userPwd);

        AssertUtil.isTrue(!userPwd1.equals(encode), "密码错误");
        UserBo userBo = new UserBo();
        BeanUtils.copyProperties(nameUser, userBo);
        return userBo;
    }
}
